package com.ho1ho.springboot.framework.core.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;

// https://stackoverflow.com/questions/1972933/cross-field-validation-with-hibernate-validator-jsr-303
// http://stackoverflow.com/questions/11890334/cross-field-validation-with-hibernatevalidator-displays-no-error-messages
// http://forum.spring.io/forum/spring-projects/web/81607-help-with-creating-custom-annotation-for-form-validation
/**
 * Validation annotation to validate that 2 fields have the same value. An array
 * of fields and their matching confirmation fields can be supplied.
 *
 * Example, compare 1 pair of fields: 
 * 
 * <pre>
 * {@code @}FieldMatch(first = "password", second = "confirmPassword", message = "The
 *                   password fields must match"),
 * {@code @}FieldMatch(first = "email", second = "confirmEmail", message = "The email
 *                   fields must match")})
 * </pre>
 */
@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = FieldMatchValidator.class)
@Documented
public @interface FieldMatch {
	String message() default "{field.not.matched}";

	Class<?>[] groups() default {};

	Class<? extends Payload>[] payload() default {};

	/**
	 * @return The first field
	 */
	String first();

	/**
	 * @return The second field
	 */
	String second();

	/**
	 * Defines several <code>@FieldMatch</code> annotations on the same element
	 * 
	 * @see FieldMatch
	 */
	@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
	@Retention(RetentionPolicy.RUNTIME)
	@Documented
	@interface List {
		FieldMatch[] value();
	}
}